package com.taijihuabao.common.activititasklistener.expensetasklistener;

import com.taijihuabao.modules.entity.DeptUserEntity;
import com.taijihuabao.modules.entity.ExpenseEntity;
import com.taijihuabao.modules.mapper.DeptUserMapper;
import com.taijihuabao.modules.mapper.ExpenseMapper;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @title: deptListener
 * @Author luoxianan
 * @Date: 2021/12/9 14:27
 * @Version 1.0
 */
@Component
public class DeptListener implements TaskListener {
    /**
     * 审核员
     */
    private final String ASSESSOR = "assessor";
    private final String TEXT= "本部门审批";
    private final String TYPE= "create";
    @Autowired
    private ExpenseMapper expenseMapper;
    @Autowired
    private DeptUserMapper deptUserMapper;
    @Override
    public void notify(DelegateTask delegateTask) {
        if (delegateTask.getName().equals(TEXT) && TYPE.equals(delegateTask.getEventName())) {
            //审批id
            String businessKey = delegateTask.getExecution().getProcessInstanceBusinessKey();
            //查询审批单
            ExpenseEntity expenseEntity = expenseMapper.selectById(Long.valueOf(businessKey));
            //查询部门所有人
            List<DeptUserEntity> applys = deptUserMapper.selectDeptByUserId(1L);
            String dept = applys.get(0).getDeptCode();
            List<DeptUserEntity> users = deptUserMapper.selectDeptByDeptCode(dept);
            //根据部门和id查询角色
            for (DeptUserEntity due : users) {
                if (ASSESSOR.equals(due.getDeptRole())) {
                    delegateTask.addCandidateUser(String.valueOf(due.getUserId()));
                }
            }
        }
    }
}
